//定义小仓库
import { defineStore } from 'pinia';
//封装的操作本地存储持久化存储token方法
import { getToken, removeToken, setToken } from '../utils/token-utils';
//引入ts数据类型
import type { UserInfoState } from './interface';
//element-ui里面提供的UI组件消息提示
import { ElMessage } from 'element-plus'
//项目路由引入仓库当中
import { staticRoutes } from '@/router/routes'
//引入接口统一管理API方法
import { reqUserLogin, reqUserInfo, reqUserLogOut } from '@/api/acl/index';
export const useUserInfoStore = defineStore('userInfo', {
  //存储数据的地方
  state: (): UserInfoState => ({
    //身份标识
    token: getToken() as string,
    //用户名字
    name: '',
    //用户头像
    avatar: '',
    //菜单路由
    menuRoutes: []
  }),

  actions: {
    //登录方法
    async login(username: string, password: string) {
      //登录请求
      const result: any = await reqUserLogin({ username, password });
      //本地持久化存储token
      setToken(result.token);
      //仓库存储一份
      this.token = result.token;
    },
    //获取用户信息
    async getInfo() {
      //获取用户信息
      const result: any = await reqUserInfo();
      //存储用户名字
      this.name = result.name;
      //存储用户头像
      this.avatar = result.avatar;
      //仓库存储全部的路由数组
      this.menuRoutes = staticRoutes;
    },
    //退出登录
    async reset() {
      //退出登录接口
      await reqUserLogOut();
      // 删除local中保存的token
      removeToken()
      // 提交重置用户信息的mutation
      this.token = ''
      this.name = ''
      this.avatar = ''
    },
  },
});
